﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BCDataCenter.BOL;

public partial class WebUserControl_KhaiBaoKhoaHuanLuyen : System.Web.UI.UserControl
{
    string error = "";
    static bool flag = false;
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetdsHRTrainCourseList();
            
        }
        
    }

    private void ClearAllText()
    {
        txtCourseID.ReadOnly = false;
        txtCourseID.Text = "";
        flag = true;
        txtDesc.Text = "";
        txtMoreDesc.Text = "";
    }

    private void GetdsHRTrainCourseList()
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "HRTrainCourseGetList", CommandType.StoredProcedure);
        if (db.Rows.Count > 0)
        {
            gvListHRTrainCourse.DataSource = db;
            gvListHRTrainCourse.DataBind();
        }
        
    }

    protected void btnNew_Click(object sender, ImageClickEventArgs e)
    {
        ClearAllText();
        
    }
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {

        if (flag)
        {
           
                BWHRTrainCourse obj = new BWHRTrainCourse();
                if (obj.Insert(ref error, txtCourseID.Text, txtDesc.Text, txtMoreDesc.Text))
                {
                    uscMsgBox1.AddMessage("Lưu khóa huấn luyện thành công", uscMsgBox.enmMessageType.Success);
                    GetdsHRTrainCourseList();
                    flag = false;
                    
                }
                else
                {
                    uscMsgBox1.AddMessage("Trùng mã khóa huấn luyện", uscMsgBox.enmMessageType.Error);                    
                }
            
        }
        else
        {
            
                BWHRTrainCourse obj = new BWHRTrainCourse();
                if (obj.Update(ref error, txtCourseID.Text, txtDesc.Text, txtMoreDesc.Text))
                {
                    uscMsgBox1.AddMessage("Cập nhật khóa huấn luyện thành công", uscMsgBox.enmMessageType.Success);
                    GetdsHRTrainCourseList();
                   
                }
                else
                {
                    uscMsgBox1.AddMessage("Mã  khóa huấn luyện không tồn tại", uscMsgBox.enmMessageType.Error);                    
                }
            
        }
    }
    protected void gvListHRTrainCourse_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName.Equals("SelectedTrainCourse"))
            {
                GridViewRow gvr = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                txtCourseID.Text = gvr.Cells[0].Text;
                DataTable db = new BWHRTrainCourse().CourseInfo(ref error, txtCourseID.Text).Tables[0];
                if (db.Rows.Count > 0)
                {
                    txtDesc.Text = db.Rows[0]["CourseDesc"].ToString();
                    txtMoreDesc.Text = db.Rows[0]["CourseMoreDesc"].ToString();
                    flag = false;
                    txtCourseID.ReadOnly = true;
                }
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    
    protected void gvListHRTrainCourse_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvListHRTrainCourse.PageIndex = e.NewPageIndex;
        GetdsHRTrainCourseList();
    }
}
